package com.example.zjhuojback.service;

import com.example.zjhuojback.entity.Problem;
import com.example.zjhuojback.entity.UserProblemStatus;
import com.example.zjhuojback.mapper.ProblemMapper;
import com.example.zjhuojback.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class ProblemService {
    @Autowired
    ProblemMapper problemMapper;

    @Autowired
    UserMapper userMapper;

    public List<Problem> selectAll() {
        return problemMapper.selectAll();
    }

    public List<Problem> selectAllAdmin() {
        return problemMapper.selectAllAdmin();
    }

    public Problem selectById(Integer id) {
        // 查询信息
        Problem dbProblem = problemMapper.selectById(id);
        return dbProblem;
    }

    public void deleteProblem(Integer id) {
        problemMapper.deleteProblem(id);
    }

    public void insertProblem(Problem problem) {
        problemMapper.insert(problem);
    }
    public void updateProblem(Problem problem) {
        problemMapper.updateProblem(problem);
    }

    public void updateSubmit(boolean ac, Integer id) {
        if (ac) { problemMapper.updateAccept(id); }
        else { problemMapper.updateSubmit(id); }
    }

    public boolean getUserProblemStatus(String userName, Integer problemId) {
        // 查询结果，如果返回值为 null，说明用户未通过此题
        UserProblemStatus exist = problemMapper.selectByUserAndProblemId(userName, problemId);
        return exist != null;
    }
    public void insertStatus(String username, Integer problemId) {
        problemMapper.insertStatus(username, problemId);
    }
}
